cmake_minimum_required(VERSION 3.5.0)
project(protobufDemo001 VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)  # 可以改为 14、17、20 等
set(CMAKE_CXX_STANDARD_REQUIRED ON)  # 强制要求 C++ 版本
set(CMAKE_CXX_EXTENSIONS OFF)  # 禁用编译器扩展

if (WIN32)
    message(STATUS "Configuring for Windows")
    set(PROTOBUF_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Depends/win/include")
    set(PROTOBUF_LIBRARY "${CMAKE_CURRENT_SOURCE_DIR}/Depends/win/lib/libprotobufd.lib")
    set(PROTOBUF_PROTOC_EXECUTABLE "${CMAKE_CURRENT_SOURCE_DIR}/Depends/win/bin/protoc.exe")
elseif (APPLE)
    set(PROTOBUF_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Depends/macOS/include")
    set(PROTOBUF_LIBRARY "${CMAKE_CURRENT_SOURCE_DIR}/Depends/macOS/lib/libprotobuf.a")
    set(PROTOBUF_PROTOC_EXECUTABLE "${CMAKE_CURRENT_SOURCE_DIR}/Depends/macOS/bin/protoc")
else()
    message(WARNING "Unknown platform")
endif()

# 指定 proto 文件
set(PROTO_SRC "${CMAKE_CURRENT_SOURCE_DIR}/addressbook.proto")

# 生成 C++ 源文件和头文件
set(PROTO_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/addressbook.pb.cc")
set(PROTO_HDRS "${CMAKE_CURRENT_SOURCE_DIR}/addressbook.pb.h")

add_custom_command(
        OUTPUT ${PROTO_SRCS} ${PROTO_HDRS}
        COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out=${CMAKE_CURRENT_SOURCE_DIR} --proto_path=${CMAKE_CURRENT_SOURCE_DIR} ${PROTO_SRC}
        DEPENDS ${PROTO_SRC}
        COMMENT "Generating C++ source files from proto"
)

message(STATUS "PROTO_SRCS: ${PROTO_SRCS}")
message(STATUS "PROTO_HDRS: ${PROTO_HDRS}")

# 创建一个生成目标
add_custom_target(generate_protos ALL DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})

# 添加可执行目标
add_executable(protobufDemo001 main.cpp  ${PROTO_SRCS} ${PROTO_HDRS})

# 包含 Protobuf 的头文件
target_include_directories(protobufDemo001 PRIVATE ${PROTOBUF_INCLUDE_DIR})

# 链接 Protobuf 库
target_link_libraries(protobufDemo001 PRIVATE ${PROTOBUF_LIBRARY})
